package com.source.baseData.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.source.baseData.vo.ToMarketGraphVO;
import com.source.baseData.entity.ToMarket;
import com.source.baseData.mapper.ToMarketMapper;
import com.source.baseData.service.ToMarketService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.source.baseData.vo.ToMarketVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Feng
 * @since 2021-02-12
 */
@Service
public class ToMarketServiceImpl extends ServiceImpl<ToMarketMapper, ToMarket> implements ToMarketService {

    @Resource
    ToMarketMapper toMarketMapper;

    @Override
    public List<ToMarketVO> find(int status) {
        return toMarketMapper.findAllVo(status);
    }

    @Override
    public Page<ToMarketVO> find(int status, int current, int size) {
        return toMarketMapper.findAllVoPage(status, new Page<>(current, size));
    }

    @Override
    public List<ToMarketVO> find(String province, String city, String district) {
        return toMarketMapper.findVoByDistrict(1, province, city, district);
    }

    @Override
    public Page<ToMarketVO> find(String province, String city, String district, int current, int size) {
        return toMarketMapper.findVoByDistrictPage(1, province, city, district, new Page<>(current, size));
    }

    @Override
    public List<ToMarketVO> findBySlaughterHouseName(String name) {
        return toMarketMapper.findVoBySlaughterHouseName(1, name);
    }

    @Override
    public Page<ToMarketVO> findBySlaughterHouseName(String name, int current, int size) {
        return toMarketMapper.findVoBySlaughterHouseNamePage(1, name, new Page<>(current, size));
    }

    @Override
    public List<ToMarketVO> findByStallName(String name) {
        return toMarketMapper.findByStallName(1, name);
    }

    @Override
    public Page<ToMarketVO> findByStallName(String name, int current, int size) {
        return toMarketMapper.findByStallNamePage(1, name, new Page<>(current, size));
    }

    @Override
    public boolean add(ToMarket toMarket) {
        if (toMarket == null) {
            return false;
        }
        toMarket.setStatus(2);
        return save(toMarket);
    }

    @Override
    public boolean modify(ToMarket toMarket) {
        if (toMarket == null || toMarket.getSlaughterHouseId() == null) {
            return false;
        }
        toMarket.setStatus(getById(toMarket.getSlaughterHouseId()).getStatus());
        return updateById(toMarket);
    }

    @Override
    public boolean deleteById(String id) {
        ToMarket toMarket = getById(id);
        toMarket.setStatus(3);
        return updateById(toMarket);
    }

    @Override
    public boolean deleteBatch(String ids) {
        return updateBatchStatusByIds(3, ids);
    }

    @Override
    public List<String> getCities(String province) {
        return toMarketMapper.getCities(1, province);
    }

    @Override
    public List<String> getDistricts(String province, String city) {
        return toMarketMapper.getDistricts(1, province, city);
    }

    @Override
    public boolean updateBatchStatusByIds(int status, String ids) {

        if (StringUtils.isEmpty(ids)) {
            return false;
        }
        ArrayList<String> idList = new ArrayList<>(Arrays.asList(ids.split(",")));
        if (idList.isEmpty()) {
            return false;
        }
        List<ToMarket> toMarkets = this.list(new QueryWrapper<ToMarket>().in("slaughterHouseId", idList));
        for (ToMarket toMarket : toMarkets) {
            toMarket.setStatus(status);
        }
        boolean res = this.updateBatchById(toMarkets);

        if (status == 0) {
            res &= this.removeByIds(idList);
        }

        return res;
    }

    @Override
    public List<ToMarketVO> like(String str) {
        return toMarketMapper.like(str);
    }

    @Override
    public Page<ToMarketVO> like(String str, int current, int size) {
        return toMarketMapper.likePage(str, new Page<>(current, size));
    }

    @Override
    public ToMarketGraphVO getPastHalfYearStallNums(String slaughterHouseId) {
        LocalDate now = LocalDate.now();
        LocalDate from = now.minusMonths(5);
        ToMarketGraphVO res = new ToMarketGraphVO();
        for (LocalDate date = from; date.compareTo(now) <= 0; date = date.plusMonths(1)) {
            int year = date.getYear();
            int month = date.getMonth().getValue();
            res.add(month, toMarketMapper.getStallNumByMonth(slaughterHouseId, year, month));
        }
        return res;
    }

    @Override
    public List<ToMarketVO> findMarketByDistrictId(String name) {
        return toMarketMapper.findMarketByDistrictId(name);
    }

    @Override
    public List<ToMarketVO> toBeReviewed() {
        return toMarketMapper.toBeReviewedList();
    }

    @Override
    public Page<ToMarketVO> toBeReviewed(int current, int size) {
        return toMarketMapper.toBeReviewedPage(new Page<>(current, size));
    }

    @Override
    public List<ToMarketVO> toBeReviewedLike(String word) {
        return toMarketMapper.toBeReviewedLikeList(word);
    }

    @Override
    public Page<ToMarketVO> toBeReviewedLike(String word, int current, int size) {
        return toMarketMapper.toBeReviewedLikePage(word, new Page<>(current, size));
    }

    @Override
    public ToMarketVO findById(String id) {
        return toMarketMapper.findById(id);
    }
}
